$(document).ready(function()
{
	
	if ($("#searchFielter").length > 0)
    {
    	$("#province").change(goSearch);
        $("#city").change(goSearch);
        $("#sex").change(goSearch);
    	if ($("#searchFielter").attr('sex'))
        {
			$("#sex").find('option[value='+$("#searchFielter").attr('sex')+']').attr('selected', true);
        }
        if ($("#searchFielter").attr('pid'))
        {
			$('#province').load('/ajax/getProvinceList/' + $("#searchFielter").attr('pid'),function(){getcitys();});
        }
    }
    else
    {
		$('#province').load('/ajax/getProvinceList');
    }
	
	if ($("#btn_batchAddFriends").length > 0)
	{
		$("#btn_batchAddFriends").click(function(){
			// 统一加关注
			if ($(this).attr('uid') <= 0)
			{
				logindialog();
				return false;
			}
			var fansurl	= '/u/' + $(this).attr('uname') + '/friends';
			var status	= new loadding($(this));
			var friends	= '';
			$('.doi').each(function(k,v)
			{
				if($(v).is(':checked') == false)
				{
					friends	+= '&fid[]='+$(v).val();
				}
			})
			if (friends != '')
			{
				$.ajax({
					type	: 'post',
					url	: '/uajax/addFriends',
					data	: friends,
					dataType	: 'json',
					beforeSend	: function()
					{
						status.loadding();
					},
					success	: function(msg)
					{
						status.stop();
						window.location.href=fansurl;
						return ;
					},
					error	: function()
					{
		                status.stop();
		                window.reload();
						return false;
					}
				})
			}
			
		})
	}
})

function insearch()
{
    var url = $("#innersearchForm").attr('action');
    if ($("#search_inpt_val").val() == '')
    {
        return false;
    }
    else
    {
    	data	= encodeURIComponent($("#search_inpt_val").val());
    	data	= data.replace(/\)/g,"%29");
    	data	= data.replace(/\(/g,"%28");
    	data	= data.replace(/\'/g,"%27");
    	data	= data.replace(/\!/g,"%21");
    	data	= data.replace(/\"/g,"%22");
    	data	= data.replace(/\+/g,"%2B");
    	data	= data.replace(/\\/g,"%5C");
        location.href= url + data;
    }
    return false;
}

function goSearch()
{
    var location_url    = '/search/user';
    if ($("#search_inpt_val").val().length > 0 )
    {
        location_url = location_url + '/' + $("#search_inpt_val").val();
    }
    if ($("#sex").val() != '' && $("#sex").val() != '不限')
    {
        location_url = location_url + '/sex/' + $("#sex").val();
    }
    if ($("#province").val() != '' && $("#province").val() != '不限')
    {
        location_url = location_url + '/province/' + $("#province").val();
    }
    if ($(this).attr('id') != 'province')
    {
        if ($("#city").val() != '' && $("#city").val() != '不限')
        {
            location_url = location_url + '/city/' + $("#city").val();
        }
    }
    if (location_url != '/search/user')window.location.href=location_url;
}

function getcitys()
{
	var pid	= $('#province').val();
	var cid = $("#searchFielter").attr('cid');
    if (pid)
	{
        $.ajax(
	    {
		    type	: 'post',
		    url	: '/ajax/getCityList/'+pid +'/' + cid,
		    success	: function(msg)
		    {
			    $('#city').html(msg);
			    //$('#city').prepend('<option value="">不限</option>');
			    //$('#city option[value=""]').attr('selected',true);
		    }
	    })
    }
    else
    {
        $('#city').html('<option value="">不限</option>');
    }
}